import 'package:enterprise_v6_app/app/components/custom_modele/custom_card.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_dropdown_selection.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/utils/file_util.dart';
import 'package:enterprise_v6_app/app/data/entities/application/major_hazard/hazard_assessment_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/assessment/assess_type.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/assessment/assessment_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/major_hazard_controller.dart';

class HazardAssessmentPage extends StatelessWidget {
  HazardAssessmentPage({required this.type, super.key});

  final controller = Get.put(HazardAssessmentController());
  MajorHazardController get hazardController => Get.find<MajorHazardController>();

  final HazardAssessmentType type;

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          body: Container(
              padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 14.w),
              alignment: Alignment.topCenter,
              color: const Color(0xffF5F5F6),
              child: Column(
                children: <Widget>[
                  GetBuilder(
                      init: hazardController,
                      builder: (_) {
                        final selected = hazardController.selectedHazard;
                        return CustomDropdownSelection(
                          text: selected?.riskObjectName,
                          hintText: '选择重大风险源',
                          onCallBack: () => hazardController.showHazardSelect(() {
                            switch (type) {
                              case HazardAssessmentType.assessment:
                                controller.keyAssessment.currentState?.onRefresh();
                              case HazardAssessmentType.evaluation:
                                controller.keyEvaluation.currentState?.onRefresh();
                            }
                          }),
                        );
                      }),
                  AppGaps.vGap10,
                  Expanded(child: CustomEasyRefreshList<HazardAssessmentEntity>(
                    key: refreshKey,
                    dataLoad: (idx, size, {search}) => controller.dataLoad(idx, size, type),
                    itemBuilder: _buildReport,
                  ),),
                ],
              )),
        );
      },
    );
  }

  Key get refreshKey {
    switch (type) {
      case HazardAssessmentType.assessment:
        return controller.keyAssessment;
      case HazardAssessmentType.evaluation:
        return controller.keyEvaluation;
    }
  }

  Widget _buildReport(HazardAssessmentEntity data, int idx) {
    return CustomCard(
      onTap: () => FileUtils.openFile(data.url ?? '', title: data.agencyName),
      padding: EdgeInsets.zero,
      titleWidget: Padding(
        padding: EdgeInsets.all(14.w),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                SvgPicture.asset(AppImages.icon_title),
                AppGaps.hGap10,
                Text(
                  data.agencyName ?? '未知',
                  style: TextStyle(fontSize: 15.w, fontWeight: FontWeight.w500),
                ),
              ],
            ),
            SvgPicture.asset(AppImages.icon_job_right),
          ],
        ),
      ),
      divider: CustomCard.builtinDivider,
      content: Padding(
        padding: EdgeInsets.all(14.w),
        child: Column(
          children: [
            _buildDetail('经办人：', data.handledBy ?? '未知'),
            AppGaps.vGap10,
            _buildDetail('联系方式：', data.mobile ?? '未知'),
            AppGaps.vGap10,
            _buildDetail('评价时间：', data.updateDate ?? '未知')
          ],
        ),
      ),
    );
  }

  Widget _buildDetail(String title, String val) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          title,
          style: TextStyle(fontSize: 13.w, color: AppColors.font_color_gray),
          textAlign: TextAlign.start,
        ),
        Text(
          val,
          style: TextStyle(fontSize: 12.w, color: AppColors.font_color_black),
          textAlign: TextAlign.end,
        ),
      ],
    );
  }
}
